<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Classe che contiene i metodi di controllo generali. Tutte le classi controller
 * ereditano da questa.
 *
 * @author andrea
 */
class Controller_vidwiz {
    
    /**
     * Esegue l'operazione passata per parametro
     * @param string $operation 
     */
    public function executeOperation($operation){
        $view = USingleton_vidwiz::getInstances("View_vidwiz");
        $debug = USingleton_vidwiz::getInstances("UDebug_vidwiz");
        switch ($operation) {
            case "vidwiz_upload_video":
                $CVideo = USingleton_vidwiz::getInstances("CVideos_vidwiz");
                $CVideo->uploadVideo();
                break;
            case "vidwiz_create_palinsesto":
                $CPal = USingleton_vidwiz::getInstances("CPalinsesto_vidwiz");
                $videos = $view->getRequest('palinsesto');
                $title = $view->getRequest('palinsestoTitle');
                $onair = $view->getRequest('onairDate');
                $CPal->createPalinsesto($videos,$onair,$title);
                break;
            case "vidwiz_breaking_news":
                $CVideo = USingleton_vidwiz::getInstances("CVideos_vidwiz");
                $CPal = USingleton_vidwiz::getInstances("CPalinsesto_vidwiz");
                $MVideo = USingleton_vidwiz::getInstances("MVideos_vidwiz");
                $positions = $CVideo->uploadVideo(true);
                $video;
                foreach($positions as $key=>$value){
                    $video = $MVideo->getVideoFromPath($value);
                    $CPal->insertBreakingNews($video);}
                break;
            case "vidwiz_insert_contest":
                $CContests = USingleton_vidwiz::getInstances("CContests_vidwiz");
                $title = $view->getRequest('title');
                $description = $view->getRequest('description');
                $startDate = $view->getRequest('startDate');
                $stopDate = $view->getRequest('stopDate');
                $type = $view->getRequest('contestType');
                $ratingDuration = $view->getRequest("ratingDuration");
                $uploadDuration = $view->getRequest("uploadDuration");
                $winnerSelection = $view->getRequest("contestType_winnerSelection");
                $CContests->insertContest($title,$startDate,$stopDate,$description,$type,$winnerSelection,$ratingDuration,$uploadDuration);
                break;
            case "vidwiz_search_videos_frontend":
                break;
            case "vidwiz_upload_video_contests":
                $CVideo = USingleton_vidwiz::getInstances("CVideos_vidwiz");
                $CContests = USingleton_vidwiz::getInstances("CContests_vidwiz");
                $MContests = USingleton_vidwiz::getInstances("MContests_vidwiz");
                $check = USingleton_vidwiz::getInstances("UChecks_vidwiz");
                $MVideo = USingleton_vidwiz::getInstances("MVideos_vidwiz");
                $view = USingleton_vidwiz::getInstances("View_vidwiz");
                $contestid = $view->getRequest("vidwiz-contest_id");
                if($check->check_canUploadVideoInContest($contestid) == true){
                    $pos = $CVideo->uploadVideo(false,false,true);
                    $vid = $MVideo->getVideoFromPath($pos);
                    $CContests->setVideoId($vid[0]['id'],$contestid);
                }
                else die("died");
            default:
                break;
        }
    }
    
    /**
     *
     * @return array_a le categorie
     */
    public function getCategories(){
        $model = USingleton_vidwiz::getInstances("MVideos_vidwiz");

        $query = $model->searchQuery("wp_terms", false, array("name","term_id"));
        $rows = $model->getRowsAssoc($query);
        return $rows;
    }

    /**
     * Converte i percorsi richiesti da relativi a assoluti (http://dominio.per/cor/so.file)
     * @param array $array l'array per il quale si richiede l'operazione
     * @param array $fields contiene i nomi (numeri) dei campi per i quali si deve rendere il percorso assoluto
     * @return array l'array con i percorsi assoluti 
     */
    public function absPathInArray($array,$fields){
        $model = USingleton_vidwiz::getInstances("Model_vidwiz");
        $debug = USingleton_vidwiz::getInstances("UDebug_vidwiz");
        foreach ($fields as $value) {
            $array[$value] = $model->getAbsPath($array[$value]);
        }
        return $array;
    }
    
    public function sortArrayCharts($a,$b){
        //impossibile
        if($a["videoid"] == $b["videoid"] && $a["scoreNr"] == $b["scoreNr"] && $a["score"] == $b["score"]) 
            return 0;
        if($a["score"] > $b["score"])
            return 1;
        if($a["scoreNr"] > $b["scoreNr"] && $a["score"] == $b["score"])
            return 1;
        if($a["videoid"] < $b["videoid"] && $a["scoreNr"] == $b["scoreNr"] && $a["score"] == $b["score"])
            return 1;
        return -1;
    }

}

?>
